/*
 * Example01.java
 *
 * Created on 19 gennaio 2008, 17.17
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package it.cnr.icib.flora2engine.examples;

import it.cnr.icib.flora2engine.Flora2;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import net.sourceforge.flora.javaAPI.src.FloraMethod;
import net.sourceforge.flora.javaAPI.src.FloraObject;

/**
 *
 * @author Nino
 */
public class Example04 {
    
    /** Creates a new instance of Example01 */
    public Example04() {
    }
    
    public static void main(String[] args) {
        
        String curDir = System.getProperty("user.dir");
        
        System.out.println(curDir);
        Flora2 session = new Flora2("/Applications/XSB/config/i386-apple-darwin11.4.0/bin",
                                    "/Applications/XSB/packages/flora2");
      
        session.setPath(curDir + "/florasrc");
        
        String fileName = "person";
        String moduleName = "test";
        
        
        if(fileName == null || fileName.trim().length() == 0) {
            System.out.println("Invalid path to example file!");
            System.exit(0);
        }
        
        session.loadFile(fileName,moduleName);
        
        /*****************************************************************
         ******** Examples of uses of the high-level Java-FLORA-2 API
         *****************************************************************/
        
        /* Printing out people's names and information about their kids
           using the high-level API. Note that the high-level person-object
           is obtained here out of the low-level FloraObject personObj
         */
        
        String  command = "?X:person@test.";
        System.out.println("Query:"+command);
        Iterator personObjs = session.ExecuteQuery(command);
       
        FloraObject personObj = new FloraObject("person",session.getFloraSession());
        
       
        person currPerson = null;
        while (personObjs.hasNext()) {
            personObj = (FloraObject)(personObjs.next());
            System.out.println("Person name:"+personObj);
            
            
            currPerson =new person(personObj,moduleName);
            Iterator kidsItr = currPerson.getLavoraCon();
            
            while(kidsItr.hasNext()) {
                FloraObject kidObj = (FloraObject)(kidsItr.next());
                System.out.println("Person Name: " + personObj
                        + " lavora con: " + kidObj);
                
                person kidPerson = null;
                kidPerson = new person(kidObj,moduleName);
                
                Iterator hobbiesItr = kidPerson.getVDN_hobbies();
                
                while(hobbiesItr.hasNext()) {
                    FloraObject hobbyObj = null;
                    hobbyObj = (FloraObject)(hobbiesItr.next());
                    System.out.println("collega:"+kidObj
                            + " has hobby: " + hobbyObj);
                }
            }
        }
        
 /*       
        FloraObject age;
        currPerson = new person("father(mary)", "example", session.getFloraSession());
        Iterator maryfatherItr = currPerson.getVDN_age();
        age = (FloraObject)maryfatherItr.next();
        System.out.println("Mary's father is " + age + " years old");
        
        currPerson = new person("mary", "example", session.getFloraSession());
        Iterator maryItr = currPerson.getVDN_age();
        age = (FloraObject)maryItr.next();
        System.out.println("Mary is " + age + " years old");
        
        // person instances using high-level interface
        person personClass = new person("person", "example", session.getFloraSession());
        instanceIter = personClass.getInstances();
        System.out.println("Person instances using high-level API:");
        while (instanceIter.hasNext())
            System.out.println("    " + (FloraObject)instanceIter.next());
        
        
        Iterator subclassIter = personClass.getSubClasses();
        System.out.println("Person subclasses using high-level API:");
        while (subclassIter.hasNext())
            System.out.println("    " + (FloraObject)subclassIter.next());
        
    */    
        // Close session and good bye
        session.close();
        System.exit(0);
    }
}
